package com.lktx.sso.admin.service;

import cn.hserver.core.ioc.annotation.Autowired;
import cn.hserver.core.ioc.annotation.Bean;
import cn.hserver.core.ioc.annotation.Value;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.RSA;
import com.lktx.sso.admin.entity.SsoApp;
import com.lktx.sso.admin.entity.SsoAppOidcConfig;
import com.lktx.sso.admin.mapper.SsoAppMapper;
import com.lktx.sso.admin.mapper.SsoAppOidcConfigMapper;
import com.lktx.sso.admin.vo.OidcConfigVO;
import com.lktx.sso.admin.vo.ProtocolEndpointVO;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * oidc配置表 服务类
 *
 */
@Slf4j
@Bean
public class SsoAppOidcConfigService {

    @Value("oauth.baseIss")
    private String baseIss;

    @Autowired
    private SsoAppOidcConfigMapper mapper;

    @Autowired
    private SsoAppMapper ssoAppMapper;

    /**
     * 分页查询列表
     *
     * @param page   分页对象
     * @param entity 查询实体
     * @return 所有数据
     */
    public Page<SsoAppOidcConfig> page(Page<SsoAppOidcConfig> page, SsoAppOidcConfig entity) {
        return mapper.paginate(page,new QueryWrapper()
                .orderBy(SsoAppOidcConfig::getSsoAppId,false)
        );
    }

    /**
     * 不分页查询列表
     *
     * @param entity 实体对象
     * @return 查询结果
     */
    public List<SsoAppOidcConfig> list(SsoAppOidcConfig entity) {
        return mapper.selectListByQuery(new QueryWrapper()
                .orderBy(SsoAppOidcConfig::getSsoAppId,false)
        );
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    public OidcConfigVO getById(Integer id) {
        SsoAppOidcConfig ssoAppOidcConfig = mapper.selectOneById(id);
        return new OidcConfigVO(ssoAppOidcConfig,new ProtocolEndpointVO(baseIss,id));
    }


    /**
     * 新增单条数据
     *
     * @param entity 实体对象
     */
    public void save(SsoAppOidcConfig entity) {
        SsoAppOidcConfig byId = getById(entity.getSsoAppId()).getSsoAppOidcConfig();
        if (byId != null) {
            if (entity.getPrivateKey() == null||entity.getPublicKey() == null) {
                RSA rsa = new RSA();
                entity.setPublicKey(rsa.getPublicKeyBase64());
                entity.setPrivateKey(rsa.getPrivateKeyBase64());
            }
            mapper.update(entity);
        }else {
            RSA rsa = new RSA();
            entity.setPublicKey(rsa.getPublicKeyBase64());
            entity.setPrivateKey(rsa.getPrivateKeyBase64());
            mapper.insert(entity);
        }
    }
    /**
     * 批量删除数据
     *
     * @param ids 主键集合
     */
    public void deleteByIds(List<Integer> ids) {
        mapper.deleteBatchByIds(ids);
    }


    public SsoAppOidcConfig getByClientId(String clientId) {
        SsoApp ssoApp = ssoAppMapper.selectOneByQuery(QueryWrapper.create().eq(SsoApp::getClientId, clientId));
        if (ssoApp == null) {
            return null;
        }
        return mapper.selectOneById(ssoApp.getSsoAppId());
    }


}